package com.sky.controller.user;

import com.sky.dto.AddressBookDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("保存地址")
    public Result<String> saveAddress(@RequestBody AddressBookDTO addressBookDTO){

        log.info("addressBookDTO={}", addressBookDTO);
        addressBookService.save(addressBookDTO);

        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("显示地址")
    public Result<List<AddressBook>> list() {

        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);

    }

    @PutMapping("/default")
    @ApiOperation("修改默认地址")
    public Result<String> setDefault(@RequestBody AddressBook addressBook){

        addressBookService.updateByDefault(addressBook);
        return Result.success();

    }

    @GetMapping("{id}")
    @ApiOperation("回显地址信息")
    public Result<AddressBook> showOne(@PathVariable Long id){

        log.info("id={}", id);
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);

    }

    @GetMapping("/default")
    @ApiOperation("显示默认地址")
    public Result<AddressBook> showDefault(){

        AddressBook addressBook = addressBookService.getByDefault();
        return Result.success(addressBook);

    }

    @DeleteMapping
    @ApiOperation("删除地址")
    public Result<String> deleteAddress(Long id){

        log.info("id={}",id);
        addressBookService.deleteById(id);
        return Result.success();

    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result<String> updateAddress(@RequestBody AddressBookDTO addressBookDTO){

        addressBookService.updateAddress(addressBookDTO);
        return Result.success();

    }

}
